package com.yzr.common.global;

import com.yzr.common.exception.BaseException;
import com.yzr.common.result.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;
import javax.security.auth.message.AuthException;

/**
 * @description
 * @author: yx-0173
 * @date: 2021-06-04 20:58
 **/
@RestControllerAdvice
@Slf4j
public class GlobalHandler {

    /**
     * 自定义异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(BaseException.class)
    public BaseResult doBaseExceptionHandler(BaseException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 参数异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BaseResult doMethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 参数异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(NullPointerException.class)
    public BaseResult doNullPointerExceptionHandler(NullPointerException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 运行时异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResult doRuntimeExceptionHandler(RuntimeException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public BaseResult doAuthExcetion(LoginException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    @ExceptionHandler(AuthException.class)
    public BaseResult doAuthExcetion(AuthException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 参数异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(BindException.class)
    public BaseResult doBindExceptionHandler(BindException e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 参数异常
     *
     * @param e 异常
     * @return BaseResult
     */
    @ExceptionHandler(Exception.class)
    public BaseResult doExceptionHandler(Exception e) {
        system(e);
        return BaseResult.buildFailure(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 输出到控制台
     *
     * @param e 异常
     */
    public void system(Throwable e) {
        StackTraceElement element = e.getStackTrace()[0];
        log.error("[异常报错信息]:{}[出现异常文件名]：{}[出现异常所在类]：{}[出现异常所在方法]：{}[出现异常具体行数]：{}",
                e.getMessage(), element.getFileName(), element.getClassName(), element.getMethodName(), element.getLineNumber());
    }
}
